package cn.edu.cdu.farmdeal.controller;

import cn.edu.cdu.farmdeal.domain.UserInf;
import cn.edu.cdu.farmdeal.service.UserService;
import cn.edu.cdu.farmdeal.utils.R;
import io.netty.util.internal.StringUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @Author: tupeng
 * @Date: 2020/3/12
 **/
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户名检查
     *
     * @param userName
     * @return
     */
    @PostMapping("/user/checkUser")
    @ResponseBody
    public boolean checkUser(String userName) {
        return userService.checkUser(userName);
    }

    /**
     * 注册
     *
     * @param userInf
     * @return
     */
    @PostMapping("/user/register")
    @ResponseBody
    public R register(@NotNull UserInf userInf) {
        String passwordMd = new Md5Hash(userInf.getPassword(), userInf.getUserid(), 1024).toHex();
        userInf.setPassword(passwordMd);
        try {
            userService.register(userInf);
        } catch (Exception e) {
            return R.error(500, "注册失败！请联系管理员");
        }
        return R.ok("恭喜！注册成功");
    }

    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestParam("userid") String userid, @RequestParam("passwd") String passwd, HttpServletRequest request,
                   ModelAndView modelAndView,HttpServletResponse response) throws Exception {
        String passwordMd = new Md5Hash(passwd, userid, 1024).toHex();
        UserInf userInf = new UserInf();
        userInf.setUserid(userid);
        String passwordDb = null;
        try {
            passwordDb = userService.getPassByName(userInf);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
            return R.error("用户名或密码错误");
        }
        HttpSession session = request.getSession();
        if ((!"".equals(passwordDb)) && (passwordDb.equals(passwordMd))) {
            session.setAttribute("userid", userid);
//            modelAndView.setViewName("index");
//            response.sendRedirect("/index.html");
//            request.getRequestDispatcher("/index.html").forward(request, response);
            return R.ok("恭喜！登录成功");
        } else if ("".equals(passwordDb)) {
            return R.error("用户名错误！请重新输入！");
        } else if (!(passwordDb).equals(passwordMd)) {
            return R.error("密码错误！请重新输入！");
        } else {
            return R.error("系统错误！请联系管理员！");
        }

    }

    @RequestMapping("/loginout.html")
    public void loginout(HttpServletRequest request,
                                 ModelAndView modelAndView,
                                 HttpServletResponse response) throws IOException, ServletException {
//        Enumeration enumeration = request.getSession().getAttributeNames();
//        while (enumeration.hasMoreElements()) {
//            request.getSession().removeAttribute(enumeration.nextElement().toString());
//        }
//        modelAndView.setViewName("index");
        request.getSession().removeAttribute("userid");
//        response.sendRedirect("index");
//        modelAndView.setViewName("index");
//        request.getRequestDispatcher("/static/login.html").forward(request, response);
        response.sendRedirect("/static/login.html");
    }
}